package com.example.wechat.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wechat.R;
import com.example.wechat.model.Sticker;

import java.util.List;

public class StickerAdapter extends RecyclerView.Adapter<StickerAdapter.ViewHolder> {
    private List<Sticker> stickers;
    private OnStickerClickListener listener;

    public interface OnStickerClickListener {
        void onStickerClick(Sticker sticker);
        void onFavoriteClick(Sticker sticker);
    }

    public StickerAdapter(List<Sticker> stickers, OnStickerClickListener listener) {
        this.stickers = stickers;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_sticker, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Sticker sticker = stickers.get(position);
        holder.stickerImage.setImageResource(sticker.getImageResId());
        holder.stickerName.setText(sticker.getName());
        holder.favoriteButton.setImageResource(
                sticker.isFavorite() ? R.drawable.ic_favorite_filled : R.drawable.ic_favorite_border
        );

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onStickerClick(sticker);
            }
        });

        holder.favoriteButton.setOnClickListener(v -> {
            if (listener != null) {
                listener.onFavoriteClick(sticker);
            }
        });
    }

    @Override
    public int getItemCount() {
        return stickers.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView stickerImage;
        TextView stickerName;
        ImageView favoriteButton;

        ViewHolder(View itemView) {
            super(itemView);
            stickerImage = itemView.findViewById(R.id.sticker_image);
            stickerName = itemView.findViewById(R.id.sticker_name);
            favoriteButton = itemView.findViewById(R.id.favorite_button);
        }
    }
} 